package org.iabako.client.ui.form.support;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.InputTextArea;
import org.iabako.client.ui.layout.field.InputTextEmail;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.iservice.support.SupportActionServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.List;

/**
 * Created by jose on 02/12/14.
 */
public abstract class AskQuestionForm extends AbstractForm {


    interface MyUiBinder extends UiBinder<HTMLPanel, AskQuestionForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    private SupportActionServiceAsync supportActionServiceAsync = SupportActionServiceAsync.Util.getInstance();

    @UiField
    InputTextEmail emailClient;
    @UiField
    InputTextArea questionBody;
    @UiField
    Button sendButton;

    public AskQuestionForm() {
        simplePopup = true;
        setForm(binderForm, PageConfigEnum.AskQuestion, null);

        sendButton.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                if (GenericTools.isEmpty(emailClient.getValue())) {
                    emailClient.addErrorMessage(emailClient, false, "validation_ask_question_no_email_error");
                    return;
                }
                if (!emailClient.isValid()) {
                    emailClient.addErrorMessage(emailClient, false, "validation_email_format_error");
                    return;
                }
                if (GenericTools.isEmpty(questionBody.getValue())) {
                    questionBody.addErrorMessage(questionBody, false, "validation_ask_question_no_question_error");
                    return;
                }

                supportActionServiceAsync.askQuestion(emailClient.getValue(), questionBody.getValue(),
                        new IabakoAsyncCallback<Void>() {
                            public void onSuccess(Void result) {
                                NavigationController.getInstance().closePopup(true);

                                NavigationController.getInstance().show(new IabakoDialogBox(
                                        "validation_ask_question_success_title",
                                        Messages.getLabel("validation_ask_question_success_body", emailClient.getValue()),
                                        true, "validation_ask_question_success_ok_button"));
                            }
                        });
            }
        });
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
